package com.leostadyn.stackfarm.timestatistics.view.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.base.BaseApplication;
import com.leostadyn.stackfarm.timestatistics.common.Constants;
import com.leostadyn.stackfarm.timestatistics.model.bean.UpdateInfoBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.UserBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;
import com.leostadyn.stackfarm.timestatistics.view.activity.LoginActivity;
import com.orhanobut.hawk.Hawk;

import java.util.HashMap;

public class SystemChangePdwActivity extends BaseActivity {
    private TextView nav_txt_title;
    private EditText et_original_password;
    private EditText et_new_password;
    private EditText et_confirm_password;
    private ImageView iv_eye_change_new;
    private ImageView iv_eye_change_sure;
    private ImageView iv_eye_change_old;
    private Boolean newChange = false;
    private Boolean oldChange = false;
    private Boolean sureChange = false;
    private Button btn_confirm;
    private UserBean userBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        findViewById(R.id.nav_img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SystemChangePdwActivity.this.finish();
            }
        });
        nav_txt_title = findViewById(R.id.nav_txt_title);
        et_original_password = findViewById(R.id.et_original_password);
        et_new_password = findViewById(R.id.et_new_password);
        et_confirm_password = findViewById(R.id.et_confirm_password);
        btn_confirm = findViewById(R.id.btn_confirm);
        iv_eye_change_new = findViewById(R.id.iv_eye_change_new);
        iv_eye_change_sure = findViewById(R.id.iv_eye_change_sure);
        iv_eye_change_old = findViewById(R.id.iv_eye_change_old);
        //userBean = (UserBean) SaveObjectTools.INSTANCE().getObjectData(Constants.USERINFO);

        et_original_password.addTextChangedListener(mTextWatcher);
        et_new_password.addTextChangedListener(mTextWatcher);
        et_confirm_password.addTextChangedListener(mTextWatcher);
        nav_txt_title.setText("修改登录密码");


        iv_eye_change_old.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oldChange == false) {
                    iv_eye_change_old.setImageResource(R.drawable.eye_open);
                    et_original_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    et_original_password.setSelection(et_original_password.length());

                } else {
                    iv_eye_change_old.setImageResource(R.drawable.login_eye);
                    et_original_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    et_original_password.setSelection(et_original_password.length());
                }
                et_new_password.requestFocus();
                oldChange = !oldChange;
            }
        });
        iv_eye_change_new.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (newChange == false) {
                    iv_eye_change_new.setImageResource(R.drawable.eye_open);
                    et_new_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    et_new_password.setSelection(et_new_password.length());

                } else {
                    iv_eye_change_new.setImageResource(R.drawable.login_eye);
                    et_new_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    et_new_password.setSelection(et_new_password.length());
                }
                et_new_password.requestFocus();
                newChange = !newChange;
            }
        });
        iv_eye_change_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sureChange == false) {
                    iv_eye_change_sure.setImageResource(R.drawable.eye_open);
                    et_confirm_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    et_confirm_password.setSelection(et_confirm_password.length());
                } else {
                    iv_eye_change_sure.setImageResource(R.drawable.login_eye);
                    et_confirm_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    et_confirm_password.setSelection(et_confirm_password.length());
                }
                et_confirm_password.requestFocus();
                sureChange = !sureChange;
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPassword = et_new_password.getText().toString().trim();
                String confirm_password = et_confirm_password.getText().toString().trim();
                String oldPassword = et_original_password.getText().toString().trim();

                if (oldPassword.equals(Hawk.get("password"))) {
                    if (!newPassword.equals(confirm_password)) {
                        ToastUtil.show(SystemChangePdwActivity.this, "两次密码不一致");
                        return;
                    }
                    SystemChangePdwActivity.this.getUpdatePassword();
                } else {
                    ToastUtil.show(SystemChangePdwActivity.this, "原密码错误");
                }
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        checkButton();

    }


    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            checkButton();
        }
    };

    private void checkButton() {
        String originalPassword = et_original_password.getText().toString().trim();
        String newPassword = et_new_password.getText().toString().trim();
        String confirm_password = et_confirm_password.getText().toString().trim();
        if (TextUtils.isEmpty(originalPassword) || TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(confirm_password)||newPassword.length()< Constants.PASSMINLENGTH||confirm_password.length()<Constants.PASSMINLENGTH||originalPassword.length()<Constants.PASSMINLENGTH) {
            btn_confirm.setEnabled(false);
        } else {
            btn_confirm.setEnabled(true);
        }
    }


    public void getUpdatePassword() {

        String oldPassword = et_original_password.getText().toString().trim();
        String password = et_new_password.getText().toString().trim();
        HashMap<String, String> map = new HashMap<String, String>();

        map.put("oldPassword", oldPassword);
        map.put("password", password);

        final KProgressHUD dialog = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setCancellable(true).setAnimationSpeed(2).setDimAmount(0.5f);
        dialog.show();

        AdminApi.getInstance().modifyPassword(map).subscribe(new OriginalSubscriber<UpdateInfoBean>(compositeDisposable) {

            @Override
            public void onError(ResponseThrowable e) {
                ToastUtil.show(SystemChangePdwActivity.this, e.getMsg());
            }

            @Override
            public void onNext(UpdateInfoBean updateInfoBean, String msg) {
                ToastUtil.show(SystemChangePdwActivity.this, "修改成功");
                Intent intent;
                intent = new Intent(SystemChangePdwActivity.this, LoginActivity.class);
                Hawk.deleteAll();
                //SaveObjectTools.INSTANCE().saveData(null, Constants.USERINFO);
                Hawk.delete("account");
                Hawk.delete("password");
                Hawk.delete("token");
                //intent.setAction(BuildConfig.APPLICATION_ID);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                setResult(Activity.RESULT_OK, intent);
                BaseApplication.getApplication().startActivity(intent);
            }
        });
    }
}
